package me.exchange.com.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.exchange.com.aop.log.Log;
import me.exchange.com.modules.system.domain.Appt;
import me.exchange.com.modules.system.dto.ApptDto;
import me.exchange.com.modules.system.dto.ApptQueryCriteria;
import me.exchange.com.modules.system.service.ApptService;
import me.exchange.com.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zhaojing
 * @create2020/5/20
 */
@Api(tags = "app管理信息表")
@RestController
@RequestMapping("/api/appt")
public class ApptController {

    @Autowired
    private ApptService apptService;

    private static final String ENTITY_NAME = "sys_appt";

    @PostMapping("/queryInfo")
    @Log("APP查询详情")
    @ApiOperation("APP查询详情")
    @PreAuthorize("@el.check('appt:list')")
    public ResponseEntity<Object> queryInfo(Integer id){

        ApptDto apptDto = apptService.findById(id);

        return new ResponseEntity<>(Result.ok("app详情查询成功").put("appt",apptDto), HttpStatus.OK);
    }

    @PostMapping("/add")
    @Log("APP新添信息")
    @ApiOperation("APP新添信息")
    @PreAuthorize("@el.check('appt:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Appt resources){

       apptService.create(resources);

        return new ResponseEntity<>(Result.ok("app管理信息添加成功"),HttpStatus.OK);
    }

    @PostMapping("/edit")
    @Log("APP修改信息")
    @ApiOperation("APP修改信息")
    @PreAuthorize("@el.check('appt:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Appt resources){

        apptService.update(resources);

        return new ResponseEntity<>(Result.ok("app管理信息修改成功"),HttpStatus.OK);
    }

    @PostMapping("/delete")
    @Log("APP删除信息")
    @ApiOperation("APP删除信息")
    @PreAuthorize("@el.check('appt:delete')")
    public ResponseEntity<Object> delete(@Validated @RequestBody Appt resources){

        apptService.delete(resources.getId());

        return new ResponseEntity<>(Result.ok("app管理信息删除成功"),HttpStatus.OK);

    }

    @PostMapping("/deleteAll")
    @Log("APP多选删除信息")
    @ApiOperation("APP多选删除信息")
    @PreAuthorize("@el.check('appt:deleteAll')")
    public ResponseEntity<Object> deleteAll(@RequestBody ApptQueryCriteria criteria){

        apptService.deleteAll(criteria.getIds());

        return  new ResponseEntity<>(Result.ok("app管理信息多选删除成功"),HttpStatus.OK);
    }

    @PostMapping("/list/download")
    @Log("导出数据")
    @ApiOperation("导出数据")
    @PreAuthorize("@el.check('appt:list')")
    public void downLoad(HttpServletResponse response,ApptQueryCriteria criteria) throws IOException {

        apptService.download(apptService.queryAll(criteria),response);

    }

    @PostMapping("/list")
    @Log("查询详情")
    @ApiOperation("查询详情")
    @PreAuthorize("@el.check('appt:list')")
    public  ResponseEntity<Object> queryAll(ApptQueryCriteria criteria){

        List<ApptDto> apptDtos = apptService.queryAll(criteria);

        return new ResponseEntity<>(Result.ok("app查询详情成功").put("apptDtod",apptDtos),HttpStatus.OK);

    }

    @PostMapping("/updateType")
    @Log("类型修改")
    @ApiOperation("类型修改")
    @PreAuthorize("@el.check('appt:edit')")
    public ResponseEntity<Object> updateType(@RequestBody ApptQueryCriteria criteria){

        apptService.updataType(criteria);

        return new ResponseEntity<>(Result.ok("APP类型修改成功"),HttpStatus.OK);

    }

    @PostMapping("/Language")
    @Log("查询语言下拉列表")
    @ApiOperation("查询语言下拉列表")
    @PreAuthorize("@el.check('appt:list')")
    public ResponseEntity<Object> queryLanguage(ApptQueryCriteria criteria){

        List<ApptDto> apptDtos = apptService.queryAll(criteria);

        return new ResponseEntity<>(Result.ok("app查询语言下拉列表"),HttpStatus.OK);
    }

}
